package com.example.leetdemo.process;

public class Solution3443 {

    public int maxDistance(String s, int k) {
        int[] forse = new int[4];
        int length = 0;
        for(int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == 'E') {
                forse[0]++;
            }
            if(s.charAt(i) == 'S') {
                forse[1]++;
            }
            if(s.charAt(i) == 'W') {
                forse[2]++;
            }
            if(s.charAt(i) == 'N') {
                forse[3]++;
            }
            int a = Math.min(forse[0], forse[2]);
            int b = Math.min(forse[1], forse[3]);
            if (a + b <= k) {
                length = i + 1;
            } else {
                int c = Math.abs(forse[0] - forse[2]) + Math.abs(forse[1] - forse[3]) + k * 2;
                length = Math.max(length, c);
            }

        }
        return length;
    }

    public static void main(String[] args) {
        Solution3443 solution3443 = new Solution3443();
//        System.err.println(solution3443.maxDistance("WESN", 1));
//        System.err.println(solution3443.maxDistance("NSWWEW", 3));
        System.err.println(solution3443.maxDistance("EWNWSWN", 1));


    }
}
